package com.zh.blog.controller.article;

import com.zh.blog.annotation.OptLog;
import com.zh.blog.common.api.Result;
import com.zh.blog.dto.ArticleBackDto;
import com.zh.blog.service.ArticleService;
import com.zh.blog.strategy.context.ArticleImportStrategyContext;
import com.zh.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import java.util.List;

import static com.zh.blog.constant.OptTypeConst.REMOVE;
import static com.zh.blog.constant.OptTypeConst.UPDATE;

/**
 * 后台文章列表控制器
 * @author ZhengHao
 * Created At 2022/8/6.
 **/
@Api(tags = "后台文章列表模块")
@RestController
public class ArticleListController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleImportStrategyContext articleImportStrategyContext;

    /**
     * 恢复或者删除文章
     * @param deleteVO 逻辑删除信息
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "恢复或者逻辑删除文章")
    @PutMapping("/admin/articles")
    public Result<?> updateArticleDelete(@Valid @RequestBody DeleteVO deleteVO){
        articleService.updateArticleDelete(deleteVO);
        return Result.ok();
    }

    /**
     * 删除文章
     * @param articleIdList 文章ID列表
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "物理删除文章")
    @DeleteMapping("/admin/articles")
    public Result<?> deleteArticles(@RequestBody List<Integer> articleIdList){
        articleService.deleteArticles(articleIdList);
        return Result.ok();
    }

    /**
     * 导出文章
     * @param articleIdList 文章ID列表
     * @return 文件URL列表
     */
    @ApiOperation(value = "导出文章")
    @ApiImplicitParam(name = "articleIdList", value = "文章ID", required = true, dataType = "List<Integer>")
    @PostMapping("/admin/articles/export")
    public Result<List<String>> exportArticles(@RequestBody List<Integer> articleIdList){
        return Result.ok(articleService.exportArticles(articleIdList));
    }

    /**
     * 导入文章
     * @param file 文件
     * @param type 文件类型
     * @return
     */
    @ApiOperation(value = "导入文章")
    @PostMapping("/admin/articles/import")
    public Result<?> importArticles(MultipartFile file, @RequestParam(required = false) String type){
        articleImportStrategyContext.importArticles(file, type);
        return Result.ok();
    }

    /**
     * 修改文章置顶状态
     * @param articleTopVO 文章置顶信息
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "修改文章置顶")
    @PutMapping("/admin/articles/top")
    public Result updateArticlesTop(@Valid @RequestBody ArticleTopVO articleTopVO){
        articleService.updateArticlesTop(articleTopVO);
        return Result.ok();
    }

    /**
     * 查看后台文章
     * @param conditionVO 提交
     * @return 后台文章列表
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/admin/articles")
    public Result<PageResult<ArticleBackDto>> listArticleBacks(ConditionVO conditionVO){
        return Result.ok(articleService.listArticleBacks(conditionVO));
    }

    /**
     * 后台根据ID查看文章
     * @param articleId 文章ID
     * @return 文章
     */
    @ApiOperation(value = "后台根据ID查看文章")
    @ApiImplicitParam(name = "articleId", value = "文章ID",required = true, dataType = "Integer")
    @GetMapping("/admin/articles/{articleId}")
    public Result<ArticleVO> getArticleBackById(@PathVariable("articleId") Integer articleId) {
        return Result.ok(articleService.getArticleBackById(articleId));
    }
}